package com.tjian.fafa.user.base.mainBody;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.config.ConfigOfFafa;
import com.tjian.fafa.user.util.image.ImageUtil;

import org.kymjs.kjframe.Core;
import org.kymjs.kjframe.bitmap.BitmapCallBack;
import org.kymjs.kjframe.utils.StringUtils;

import java.util.List;

import static android.R.attr.bitmap;

public class FriendListAdapter extends BaseExpandableListAdapter
{

    private Context mContext;
    private LayoutInflater mInflater = null;
    private List<FriendListGroupItem> mGrpInfo = null;
    private List<List<UserInfo>> mUsrInfo = null;
    float roundPx_class = 120; //图片变圆角参数；

    public String getImgpath()
    {
        return imgpath;
    }

    public void setImgpath(String imgpath)
    {
        this.imgpath = imgpath;
    }

    private String imgpath = "";

    public FriendListAdapter(Context ctx, List<FriendListGroupItem> listGrp, List<List<UserInfo>> listUsr)
    {
        mContext = ctx;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mGrpInfo = listGrp;
        mUsrInfo = listUsr;
    }

    public void setData(List<List<UserInfo>> list)
    {
        mUsrInfo = list;
    }

    @Override
    public int getGroupCount()
    {

        return mUsrInfo.size();
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {

        return mUsrInfo.get(groupPosition).size();
    }

    @Override
    public List<UserInfo> getGroup(int groupPosition)
    {

        return mUsrInfo.get(groupPosition);
    }

    @Override
    public UserInfo getChild(int groupPosition, int childPosition)
    {

        return mUsrInfo.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition)
    {

        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {

        return childPosition;
    }

    @Override
    public boolean hasStableIds()
    {

        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent)
    {

        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.cc0_friend_list_group, null);
        }
        GroupViewHolder holder = new GroupViewHolder();

        holder.groupindicator_imageview = (ImageView) convertView
                .findViewById(R.id.groupindicator_imageview_id);

        //判断isExpanded就可以控制是按下还是关闭，同时更换图片
        if (isExpanded)
        {
            holder.groupindicator_imageview.setBackgroundResource(R.drawable.team_into2_n);
        }
        else
        {
            holder.groupindicator_imageview.setBackgroundResource(R.drawable.team_into1_n);
        }

        holder.mGroupName = (TextView) convertView
                .findViewById(R.id.cc0_friend_list_group_name);
        holder.mGroupName.setText(mGrpInfo.get(groupPosition).getName());
        holder.mGroupCount = (TextView) convertView
                .findViewById(R.id.cc0_friend_list_user_count);
        holder.mGroupCount.setText("" + mUsrInfo.get(groupPosition).size() + "");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {

        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.cc0_friend_list_item, null);
        }
        ChildViewHolder holder = new ChildViewHolder();
        int isOnline = getChild(groupPosition, childPosition).getIsOnline();

        holder.mIcon = (ImageView) convertView.findViewById(R.id.cc0_friend_list_item_avatar);
        String avatarPath = getChild(groupPosition, childPosition).getmAvatarImagePath();

        UserInfo userInfo = getChild(groupPosition, childPosition);
        String urlString = ConfigOfFafa.projectWebsiteUrl +"/"+ avatarPath;

        //在线
        if (isOnline == 1)
        {
            setAvatarUrl_fafa(holder,urlString);
        }
        //不在线
        else
        {
            setAvatarUrl_fafa_offline(holder,urlString);
        }
        holder.mChildName = (TextView) convertView.findViewById(R.id.cc0_friend_list_item_name);
        holder.mChildName.setText(getChild(groupPosition, childPosition).getName());
        return convertView;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer)
    {
        if (observer != null)
        {
            super.unregisterDataSetObserver(observer);
        }
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {

        /*很重要：实现ChildView点击事件，必须返回true*/
        return true;
    }

    private class GroupViewHolder
    {
        ImageView groupindicator_imageview;
        TextView mGroupName;
        TextView mGroupCount;
    }

    private class ChildViewHolder
    {
        ImageView mIcon;
        TextView mChildName;
    }


    public void setAvatarUrl_fafa(final ChildViewHolder holder, String url)
    {

        holder.mIcon.setImageResource(R.drawable.default_avatar_icon);
        Bitmap bitmapLocal = ((BitmapDrawable) holder.mIcon.getDrawable()).getBitmap();
        bitmapLocal = ImageUtil.convertToBlackWhite(bitmapLocal);//不在线头像变灰色；
        bitmapLocal = ImageUtil.convertToRoundedCorner(bitmapLocal, roundPx_class);
        holder.mIcon.setImageBitmap(bitmapLocal);

        if (StringUtils.isEmpty(url))
        {
            return;
        }
        // 由于头像地址默认加了一段参数需要去掉
        int end = url.indexOf('?');
        final String headUrl;
        if (end > 0)
        {
            headUrl = url.substring(0, end);
        }
        else
        {
            headUrl = url;
        }

        Core.getKJBitmap().display(holder.mIcon, headUrl, R.drawable.widget_dface, 0, 0,
                new BitmapCallBack()
                {
                    @Override
                    public void onFailure(Exception e)
                    {
                        super.onFailure(e);
                        holder.mIcon.setImageResource(R.drawable.default_avatar_icon);
                    }
                });
    }

    public void setAvatarUrl_fafa_offline(final ChildViewHolder holder, String url)
    {
        holder.mIcon.setImageResource(R.drawable.default_avatar_icon);
        Bitmap bitmapLocal = ((BitmapDrawable) holder.mIcon.getDrawable()).getBitmap();
        bitmapLocal = ImageUtil.convertToBlackWhite(bitmapLocal);//不在线头像变灰色；
        bitmapLocal = ImageUtil.convertToRoundedCorner(bitmapLocal, roundPx_class);
        holder.mIcon.setImageBitmap(bitmapLocal);

        if (StringUtils.isEmpty(url))
        {
            return;
        }
        // 由于头像地址默认加了一段参数需要去掉
        int end = url.indexOf('?');
        final String headUrl;
        if (end > 0)
        {
            headUrl = url.substring(0, end);
        }
        else
        {
            headUrl = url;
        }

        Core.getKJBitmap().display(holder.mIcon, headUrl, R.drawable.widget_dface, 0, 0,
                new BitmapCallBack()
                {
                    @Override
                    public void onFailure(Exception e)
                    {
                        super.onFailure(e);
                        holder.mIcon.setImageResource(R.drawable.default_avatar_icon);
                    }
                });
    }

}